from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import os
from selenium.webdriver.common.keys import Keys
import time
import xlrd
import xlwt


def phantom_driver():
    driver_path = os.path.abspath('phantomjs')
    return webdriver.PhantomJS(driver_path)


def chrome_driver():
    driver_path = os.path.abspath('chromedriver')
    return webdriver.Chrome(driver_path)


table = xlwt.Workbook()
sheet = table.add_sheet("sheet0", True)
filePath = '/Volumes/WWW/123.xls'

browser = chrome_driver()

browser.get('https://order.mi.com/user/address?r=12181.1517889529')
browser.find_element_by_xpath('//*[@id="username"]').send_keys('17186761918')
browser.find_element_by_xpath('//*[@id="pwd"]').send_keys('song.1718')
browser.find_element_by_xpath('//*[@id="login-button"]').click()

browser.implicitly_wait(2)

# 点击收货地址
# browser.find_element_by_xpath('//*[@id="J_newAddress"]').click()
action = ActionChains(browser)
action.click(browser.find_element_by_xpath('//*[@id="J_newAddress"]')).perform()

# browser.implicitly_wait(5)
# 点击选择省市区 //*[@id="J_selectAddressTrigger"]
# action.click(browser.find_element_by_xpath('//*[@id="J_modalEditAddress"]/div[2]/div[1]/div[3]')).perform()
# browser.find_element_by_xpath('//*[@id="J_selectAddressTrigger"]').click()
browser.implicitly_wait(5)
show_js = 'document.getElementById("J_selectAddressWrapper").className = "select-address-wrapper";' \
          'document.getElementById("J_selectAddressWrapper").getElementsByTagName("div")' \
          '[10].className ="four-address-wrapper J_selectAddressItem";'
browser.execute_script(show_js)

zhixia = ['北京', '天津', '重庆', '上海']


num = 0

for i in range(1, 32):
    try:
        # //*[@id="J_fourAddressWrapper"]/div[2]/ul[1]/li[1]
        path = '//*[@id="J_fourAddressWrapper"]/div[2]/ul[1]/li[' + str(i) + ']'
        ee = browser.find_element_by_xpath(path)
        c1 = ee.get_attribute('data-txt')

        print(c1)
        if zhixia.__contains__(c1):
            continue

        action = ActionChains(browser)
        action.click(ee).perform()
        browser.implicitly_wait(4)

        for j in range(1, 300):
            try:
                # //*[@id="J_fourAddressWrapper"]/div[2]/ul[2]/li[1]
                # //*[@id="J_fourAddressWrapper"]/div[2]/ul[2]/li[1]
                path = '//*[@id="J_fourAddressWrapper"]/div[2]/ul[3]/li[' + str(j) + ']'
                ee = browser.find_element_by_xpath(path)
                c2 = ee.get_attribute('data-txt')
                action = ActionChains(browser)
                action.click(ee).perform()
                browser.implicitly_wait(4)

                for k in range(1, 300):
                    try:
                        # //*[@id="J_fourAddressWrapper"]/div[2]/ul[4]/li[1]
                        path = '//*[@id="J_fourAddressWrapper"]/div[2]/ul[4]/li[' + str(k) + ']'
                        ee = browser.find_element_by_xpath(path)
                        c3 = ee.get_attribute('data-txt')
                        action = ActionChains(browser)
                        action.click(ee).perform()
                        browser.implicitly_wait(4)

                        for m in range(1, 300):
                            try:
                                # //*[@id="J_fourAddressWrapper"]/div[2]/ul[4]/li[1]
                                path = '//*[@id="J_fourAddressWrapper"]/div[2]/ul[4]/li[' + str(m) + ']'
                                c4 = browser.find_element_by_xpath(path).get_attribute('data-txt')

                                print(c1, c2, c3, c4)
                                sheet.write(num, 0, c1)
                                sheet.write(num, 1, c2)
                                sheet.write(num, 2, c3)
                                sheet.write(num, 3, c4)
                                num += 1
                                table.save(filePath)

                            except (BaseException) as e:
                                print('c4 ------')
                                print(e)
                                break

                    except (BaseException) as e:
                        print('c3 ------')
                        print(e)
                        break

            except (BaseException) as e:
                print('c2 ------')
                print(e)
                break
    except (BaseException) as e:
        print('c1 ------')
        print(e)
        break